<?php

/*
    URL test-case
    
        Author:     Alfredo Mungo
        License:    LGPL
        Copyright:  2012
*/

define('TEST_TITLE', 'URL class');

include 'test_suit.php';
include '../modules/URL.php';

function test1() {
    $url_s = "https://icecoder:mysecret@www.example.com:379/%2E%41%42%43.txt?date=now&sex=F#home";
    $url = new URL($url_s);
    
    test_report_add_result("getScheme()", $url->getScheme() == 'https');
    test_report_add_result("getHost()", $url->getHost() == 'www.example.com');
    test_report_add_result("getPort()", $url->getPort() === 379);
    test_report_add_result("getUser()", $url->getUser() == 'icecoder');
    test_report_add_result("getPassword()", $url->getPassword() == 'mysecret');
    test_report_add_result("getPath()", $url->getPath() == '/.ABC.txt');
    test_report_add_result("getFragment()", $url->getFragment() == 'home');
    test_report_add_result("getQuery()", $url->getQuery('date') == 'now' && $url->getQuery('sex') == 'F' && count($url->getQuery()) == 2);
    test_report_add_result("getLongQuery()", $url->getLongQuery() == 'date=now&sex=F');
    test_report_add_result("getOriginal()", $url->getOriginal() == $url_s);
}

function t_test() {
    $url_s = "?index=&tablet#";
    $url = new URL($url_s);
    
    test_report_add_result("getScheme()", $url->getScheme() == null);
    test_report_add_result("getHost()", $url->getHost() == null);
    test_report_add_result("getPort()", $url->getPort() === 80);
    test_report_add_result("getUser()", $url->getUser() == null);
    test_report_add_result("getPassword()", $url->getPassword() == null);
    test_report_add_result("getPath()", $url->getPath() == null);
    test_report_add_result("getFragment()", $url->getFragment() == null);
    test_report_add_result("getQuery()", $url->getQuery('date') == null && $url->getQuery('index') == '' && $url->hasParam('tablet'));
    
}

function test_normalize()
{
    $url = "hTTp://server.example.coM:8080/pages/../../../../pages/.home/~icecoder/docs/../images/././test_image@24.jpg?width=800&height=600&token=$#last-pixel";
    
    test_report_add_result("URL with custom port, dotted path and special symbols", URL::normalizeURL($url) == 'http://server.example.com:8080/pages/.home/~icecoder/images/test_image%4024.jpg', URL::normalizeURL($url));
    test_report_add_result("URL with custom port, dotted path and special symbols (with query)", URL::normalizeURL($url, false) == 'http://server.example.com:8080/pages/.home/~icecoder/images/test_image%4024.jpg?width=800&height=600&token=%24', URL::normalizeURL($url, false));
    test_report_add_result("URL with custom port, dotted path and special symbols (with fragment)", URL::normalizeURL($url, true, false) == 'http://server.example.com:8080/pages/.home/~icecoder/images/test_image%4024.jpg#last-pixel', URL::normalizeURL($url, true, false));
    test_report_add_result("URL with custom port, dotted path and special symbols (with query&fragment)", URL::normalizeURL($url, false, false) == 'http://server.example.com:8080/pages/.home/~icecoder/images/test_image%4024.jpg?width=800&height=600&token=%24#last-pixel', URL::normalizeURL($url, false, false));
    
    $url = 'http://example.com?%38_kill=true';
    
    test_report_add_result("URL with std port and encoded symbols", URL::normalizeURL($url) == 'http://example.com', URL::normalizeURL($url));
    test_report_add_result("URL with std port and encoded symbols (with query)", URL::normalizeURL($url, false) == 'http://example.com?%2538_kill=true', URL::normalizeURL($url, false));
    test_report_add_result("URL with std port and encoded symbols (with fragment)", URL::normalizeURL($url, true, false) == 'http://example.com', URL::normalizeURL($url, true, false));
    test_report_add_result("URL with std port and encoded symbols (with query&fragment)", URL::normalizeURL($url, false, false) == 'http://example.com?%2538_kill=true', URL::normalizeURL($url, false, false));
    
}

test_register('test1', "Methods");
test_register('t_test', "Tolerance");
test_register('test_normalize', 'Normalization');
test_run();

include 'test_template.php'

?>